import pygame, script, projectile
import particle as part
import tile_tools as tt
import math as Math

class Enemy:
	def __init__(self,map_num,id,x,y,eset,col):
		self.checked = 0
		self.col = col
		self.id = id
		self.x = x
		self.y = y
		escript = script.Script("resources/map"+str(map_num)+"/ene.txt")
		self.surf = 0
		self.eset = eset
		self.health = 100
		self.cur_frame = 0
		self.start_particle = 0
		self.part_group = 0
		self.points_worth = 0
		self.health_surf = pygame.Surface((50,8))
		self.health_surf_bg = pygame.Surface((10,4))
		self.health_surf.set_colorkey((255,0,255))
		self.health_surf_bg.fill((255,0,0))
		self.health_surf.fill((0,255,0))
		
	def is_dead(self,ply):
		if self.health < 0:
			if self.start_particle == 0:
				ply.points = ply.points + self.points_worth
			#	self.surf.fill((255,0,255))
				self.start_particle = 1
			#if self.start_particle == 1:
			#	self.part_group = part.Particle_Group(self.x + self.w/2,self.y + self.h/2,100,(255,0,0))
			#	self.start_particle = -1
			#if self.start_particle == -1:
			#	self.part_group.update()
			#	self.surf = self.part_group.draw(self.surf)
			return True
		else:
			return False
	
	def set_dim(self,w,h):
		self.w = w
		self.h = h
	
	def draw(self,ply):
		if self.is_dead(ply) == False:
			self.surf = self.img[self.cur_frame]
		return self.surf
	
	def collide_player(self,ply):
		B1 = pygame.Rect((ply.x,ply.y),(64,64))
		B2 = pygame.Rect((self.x,self.y),(self.w,self.h))
		if B1.colliderect(B2):
			ply.health = ply.health - 1
	
	def draw_health_bar(self):
		reduced = self.health*80 /  100
		pygame.draw.rect(self.health_surf,(255,0,0),(0,0,80,8))
		pygame.draw.rect(self.health_surf,(0,255,0),(0,0,reduced,8))
		return self.health_surf
		
		
class moving_gunner(Enemy):
	def __init__(self,map_num,id,x,y,eset,col,rng):
		Enemy.__init__(self,map_num,id,x,y,eset,col)
		self.img = []
		self.surf = pygame.Surface((32,32))
		self.rng = rng
		for i in range(0,1):
			self.img.append(tt.get_tile_graphic(i,32,32,eset))
		self.set_dim(self.img[0].get_width(),self.img[0].get_height())
		self.plist = []
		self.max_frame = 1
		self.points_worth = 10
		self.move_cnt = 0
		self.move_const = 1
		self.range = 10
		
		
	def move(self):
		self.move_cnt = self.move_cnt + 5*abs(self.move_const)
		self.x = self.x + 5*self.move_const
		if self.move_cnt > self.rng:
			self.move_const = self.move_const*-1
			self.move_cnt = 0
			
		
	def shoot(self,map,time,delay,ply):
		self.move()
		if time % delay == 0:
			tmpp = projectile.Projectile("bullet",10,"Enemy",False,100,0,-10,self.x,self.y)
			self.plist.append(tmpp)
		for proj in self.plist:
			if proj.move() == 0:
				self.plist.remove(proj)
			if proj.check_collision(map,ply) == 1:
				self.plist.remove(proj)
		
class lrgunner(Enemy):
	def __init__(self,map_num,id,x,y,eset,col):
		Enemy.__init__(self,map_num,id,x,y,eset,col)
		self.img = []
		self.surf = pygame.Surface((64,64))
		for i in range(0,2):
			self.img.append(tt.get_tile_graphic(i,64,64,eset))
		self.set_dim(self.img[0].get_width(),self.img[0].get_height())
		self.plist = []
		self.max_frame = 2
		self.points_worth = 5
		
	def move(self,ply):
		if ply.x >= self.x:
			self.cur_frame = 1
		else:
			self.cur_frame = 0
		
		
	def shoot(self,map,time,delay,ply):
		self.move(ply)
		if time % delay == 0:
			if ply.x >= self.x:
				tmpp = projectile.Projectile("bullet",5,"Enemy",False,100,10,0,self.x,self.y)
			else:
				tmpp = projectile.Projectile("bullet",5,"Enemy",False,100,-10,0,self.x,self.y)
			self.plist.append(tmpp)
		for proj in self.plist:
			if proj.move() == 0:
				self.plist.remove(proj)
			if proj.check_collision(map,ply) == 1:
				self.plist.remove(proj)
				
				
class soldier(Enemy):
	def __init__(self,map_num,id,x,y,col):
		Enemy.__init__(self,map_num,id,x,y,0,col)
		self.img_left = []
		self.img_right = []
		self.dir = "left"
		self.surf = pygame.Surface((64,64))
		for i in range(0,10):
			self.img_right.append(tt.get_tile_graphic(i,64,64,pygame.image.load("resources/esrt/soldierrht.bmp").convert()))
		for i in range(0,10):
			self.img_left.append(tt.get_tile_graphic(i,64,64,pygame.image.load("resources/esrt/soldierlft.bmp").convert()))
		self.set_dim(self.img_left[0].get_width(),self.img_left[0].get_height())
		self.plist = []
		self.max_frame = 2
		self.points_worth = 5
		self.cur_frame = 0
	
	def in_view(self,ply,v):
		if ply.x - v < self.x and ply.x + v > self.x:
			return True
		else:
			return False
			
	def move(self,ply,time,delay):
		if self.in_view(ply,1000) == True:
			self.cur_frame += 1
			if self.cur_frame >= 9:
				self.cur_frame = 0
			if ply.x > self.x:
				self.dir = "right"
				self.x = self.x + 5
			else:
				self.dir = "left"
				self.x = self.x - 1
	
	def draw(self,ply):
		if self.is_dead(ply) == False:
			if self.dir == "right":
				self.surf = self.img_right[self.cur_frame]
			else:
				self.surf = self.img_left[self.cur_frame]
		return self.surf
		
	def shoot(self,map,time,delay,ply):
		delay = 10
		hyp = Math.sqrt(((self.x - ply.x )**2) + ((self.y - ply.y)**2))
		if hyp == 0:
			hyp = 1
		leg = abs(self.y - ply.y)
		angley = Math.asin(leg/hyp)
		anglex = Math.acos(leg/hyp)
		self.move(ply,time,delay)
		if ply.x > self.x and ply.y < self.y:
			anglex = abs(anglex)
			angley = angley*-1
		elif ply.x > self.x and ply.y > self.y:
			anglex = abs(anglex)
			angley = abs(angley)
		elif ply.x < self.x and ply.y >= self.y:
			anglex = anglex*-1
			angley = abs(angley)
		elif ply.x < self.x and ply.y < self.y:
			anglex = anglex*-1
			angley = angley*-1
		if time % delay == 0:
			tmpp = projectile.Projectile("bullet",5,"Enemy",False,100,10*anglex,10*angley,self.x,self.y)
			self.plist.append(tmpp)
		for proj in self.plist:
			if proj.move() == 0:
				self.plist.remove(proj)
			if proj.check_collision(map,ply) == 1:
				self.plist.remove(proj)